package com.example.Nurse_Notifier;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.ToggleButton;

public class DeviceActivity extends Activity {

    private int currentStatus = -1;
    private static int status = -1;
    private static int devNum = -1;
    private Intent intent;
    private static boolean isChanged = false;
    private boolean isToggled = false;
    int colorGreen;
    int colorRed;
    int colorBlank;
    ToggleButton toggleButton;
    TextView nextStatusTV;

    String[] statuses = {
            "выключено",
            "простаивает",
            "сканирует",
            "оповещает"
    };
    Button pressButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.device_verbose_layout);

        colorGreen = getResources().getColor(R.color.dark_green);
        colorRed = getResources().getColor(R.color.test_color_red);
        colorBlank = getResources().getColor(R.color.test_color_gray);

        intent = this.getIntent();
//        TextView numTextView = (TextView) findViewById(R.id.deviceNumSettedTV);

        devNum = Integer.valueOf(intent.getStringExtra("devNum"));
        TextView macTV = (TextView) findViewById(R.id.deviceMacTV);
        macTV.setText(macTV.getText() + "  " + intent.getStringExtra("devMac"));
        final TextView statusTV = (TextView) findViewById(R.id.deviceStatusText);
        statusTV.setText(intent.getStringExtra("devStatus"));
        currentStatus = intent.getIntExtra("intStatus", -1);
        nextStatusTV = (TextView) findViewById(R.id.blankTV);
        toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
        customizeView(currentStatus);

        pressButton = (Button) findViewById(R.id.deviceActivityPressButton);
        pressButton.setEnabled(false);
        pressButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isChanged = true;
                statusTV.setText(statuses[status]);
                currentStatus = status;
                try {
                    Thread.sleep(750);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                finish();
            }
        });

        toggleButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (currentStatus != UdpServer.DEVICE_OFF) {
                    switch (currentStatus) {
                        case UdpServer.DEVICE_IDLE: {
                            if (!isToggled) {
                                toggleButton.setBackgroundColor(colorRed);
                                status = UdpServer.DEVICE_SCAN;
                                isToggled = true;
                                pressButton.setEnabled(true);
                                nextStatusTV.setText("Устройство будет включено");
                            } else {
                                toggleButton.setBackgroundColor(colorGreen);
                                status = UdpServer.DEVICE_IDLE;
                                isToggled = false;
                                pressButton.setEnabled(false);
                                nextStatusTV.setText("Нажимите кнопку ВКЛ/ВЫКЛ");
                            }
                            break;
                        }

                        case UdpServer.DEVICE_SCAN: {
                            if (!isToggled) {
                                toggleButton.setBackgroundColor(colorGreen);
                                status = UdpServer.DEVICE_IDLE;
                                isToggled = true;
                                pressButton.setEnabled(true);
                                nextStatusTV.setText("Устройство будет выключено");
                            } else {
                                toggleButton.setBackgroundColor(colorRed);
                                status = UdpServer.DEVICE_SCAN;
                                isToggled = false;
                                pressButton.setEnabled(false);
                                nextStatusTV.setText("Нажимите кнопку ВКЛ/ВЫКЛ");
                            }
                            break;
                        }
                    }
                }
            }
        });
    }

    public static int[] getData() {
        int res[] = {devNum, status};
        return res;
    }

    public static boolean isStatusChanged() {
        return isChanged;
    }

    public static void setIsChanged(boolean isChanged) {
        DeviceActivity.isChanged = isChanged;
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    public void customizeView(int status) {
        switch (status) {
            case UdpServer.DEVICE_IDLE: {
                toggleButton.setChecked(true);
                toggleButton.setBackgroundColor(colorGreen);
                break;
            }

            case UdpServer.DEVICE_SCAN: {
                toggleButton.setChecked(false);
                toggleButton.setBackgroundColor(colorRed);
                break;
            }

            case UdpServer.DEVICE_OFF: {
                toggleButton.setEnabled(false);
                break;
            }

            case UdpServer.DEVICE_ALARM: {
                toggleButton.setBackgroundColor(colorRed);
                break;
            }
        }
    }

//  public void onActivityResult(int requestCode, int resultCode, Intent intent) {
//        if (requestCode == 0) {
//            if (resultCode == RESULT_OK) {
//                Log.i("SK", "Result isok,go further and see what happens");
//                String contents = intent.getStringExtra("SCAN_RESULT");
//                String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
//                Log.i("SK", "contents: " + contents + " format: " + format);
//            } else if (resultCode == RESULT_CANCELED) {
//                Log.i("SK", "Cancelled");
//            }
//        }
//    }
}
